home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Libris Britannia 4
/
science library(b).zip
/
science library(b)
/
PROGRAMM
/
CC_C
/
0151.ZIP
/
INDEX.C
< prev
next >
Wrap
C/C++ Source or Header
|
1985-08-15
|
15KB
|
551 lines
/********************************************************
* *
* INDEX *
* Text file index generator. *
* *
* T. Jennings 7/21/81 *
* MSDOS version 13 Nov. 82 *
* Repaired 30 Aug 84 *
* *
*********************************************************
INDEX Generates an ASCII WordStar (or equiv.) compatible
index from a text file. Words to be indexed are marked
with a control character. Entire phrases can be indexed
as well, by marking both ends with a different character.
The index generated will be sorted alphabetically, with
the first character of all entries in caps. Each entry
will contain the page number as well.
Two control characters, ^K and ^P are used. ^K marks
single words, and ^P marks phrases. Phrases too long
will be truncated to fit.
Examples:
The sixth word in this ^Ksentence will be put in the index.
^PThis entire phrase^P will be indexed.
The index for these two items, once printed, would look
like:
Sentence .................................... 2
This entire phrase .......................... 3
See INDEX.DOC for details
*/
#include <stdio.h>
#include <ctype.h>
char *getmem();
long sizmem();
long _xseek();
char *strip_path();
char *next_arg();
#define MAXENTRIES 10000
#define FALSE 0
#define TRUE 1
#define ERROR -1
#define WORD_MARK 0x0b /* The single word marker */
#define PHRASE_MARK 0x10 /* Phrase marker */
#define CONTROLZ 0x1a
#define CR 0x0d /* useful ASCII characters */
#define LF 0x0a
#define FF 0x0c
#define WORD_LEN 39 /* maximum word or phrase size */
#define LAST_COL 40 /* column to start page # */
#define PAGE_LEN 66 /* default lines per page */
#define DEF_LMARGIN 8 /* default left margin, */
#define DEF_RMARGIN 72 /* ... and right margin */
#define DEF_TMARGIN 1 /* default top of page margin */
#define DEF_BMARGIN 8
int char_count; /* # characters looked at in file, */
int word_count; /* # words looked at */
int line_count; /* ditto lines */
int this_line; /* current line #/page */
int entries; /* # entries in index */
int this_page; /* current page */
int page_size; /* current max page length */
int lmargin,rmargin; /* current left and right margins */
int top_margin; /* lines from top of page */
int bot_margin;
int found_index; /* true if old index found */
int line_num; /* true if line numbers in index */
int tofc; /* generate table of contents */
int inbuf; /* text file */
char inname[80]; /* where we save ASCII filenames */
char *pile; /* ptr to text buffer, */
char *topofpile; /* the top of the pile */
long memsize; /* how big it is */
long currsize; /* current size */
char *pointers[MAXENTRIES]; /* pile pointers for sorting */
char pl; /* current index to */
/* System interface for INDEX. Make sure theres at least one argument (the
filename). Open it for reading, (error check) make a temporary file for
output (filename.$I$) (error check). Then...
1/ Read the file sequentially, looking for marked words, [ index() ]
2/ Cleanup the pile of entries (remove leading blanks, convert each
1st character to uper case) [ cleanup() ]
3/ Sort the pile alphabeticlly [ sort() ]
4/ Dump the pile to the disk, expanding each to correct width,
and removing duplicate entries [ dump() ]
5/ Return to CP/M
*/
main(argc,argv)
int argc;
char *argv[];
{
char *s,*p,sw[80],arg[80];
int i;
printf("INDEX -- Document Index Generator (c) Fido Software 1981, 1982, 1983, 1984, 1985\r\n");
page_size= PAGE_LEN;
lmargin= DEF_LMARGIN;
rmargin= DEF_RMARGIN;
top_margin= DEF_TMARGIN;
bot_margin= DEF_BMARGIN;
found_index= FALSE;
line_num= FALSE;
tofc= FALSE;
/* Try to allocate a bunch of memory. If theres not enough, say so. */
allmem(); /* find largest area to allocate */
memsize= sizmem();
if (memsize < 4096L) { /* stop if not enough */
printf("Oh no, not enough memory! Less than");
printf("4096 bytes to work in!\r\n");
exit(1);
}
pile= getmem(memsize);
topofpile= pile;
*pile= '\0';
currsize= 0L;
*inname= '\0';
/* Process the command line. We must have a filename, and optionally one
of two options: list amount of memory, of add line numbers to the index. */
for (i= 1; i < argc; i++) { /* Process any options, */
p= argv[i];
cpyarg(arg,p); /* possible name */
if (strlen(inname) == 0) strcpy(inname,arg);
strip_switch(sw,p); /* process switches, */
s= sw;
while (*s) {
switch(tolower(*s)) {
case 'L':
printf("Putting line numbers in the index.\r\n");
line_num= TRUE;
break;
case '?':
printf("INDEX has %lu bytes to work in.\r\n",memsize);
break;
case 'T':
printf("Table of Contents (TOFC.INS)\r\n");
tofc= 1;
break;
}
++s;
}
}
if (strlen(inname) == 0) {
printf("Specify a file to make an index for\r\n");
exit(1);
}
inbuf= _xopen(inname,2); /*try to open source file,*/
if (inbuf == ERROR) {
printf("Can't find %s\r\n",inname);
exit(1);
}
printf("Adding a new index to '%s'.\r\n",inname);
printf("1) Scanning,\r\n");
index(); /* scan the file, */
cleanup(); /* convert each 1st char to upper case */
printf ("2) Sorting,\r\n");
sort(); /* sort it, */
printf ("3) Saving it,\r\n");
dump(); /* write it to the disk, */
printf ("4) Cleaning up.\r\n");
_xclose(inbuf);
printf ("done.\r\n");
printf("Put %d words in the index ",entries);
printf("out of a total of %d words.\r\n",word_count);
exit(0); /* exit. */
}
/* Read the input file, and make a list of words to index. Maintain
the global variables indicating word count, etc. Leave a pile of
strings, followed by the page #, terminated with a control-z.
When done, PILE[] will have sequential null terminated strings, terminated
by a single control-z. Each pointer in POINTERS[] will point to the start
of each string, with the last pointing to the control-z.
*/
index() {
int inword; /* blank or character flag */
int gotword; /* true if saving this word */
int gotphrase; /* true if saving this phrase */
int entry_len; /* size of word or phrase */
char linebuf[132]; /* character line buffer */
int i,j;
int last_char_blank; /* suppress mult. spaces 'tween lines */
char c;
inword= FALSE; /* no word yet, */
gotword= FALSE; /* no marked word found, */
gotphrase= FALSE; /* no marked phrase found, */
last_char_blank= FALSE; /* too early... */
pl= 0; /* current index */
char_count= 0;
word_count= 0; /* and our booleans */
line_count= 0;
entries= 0;
this_page= 1;
this_line= top_margin; /* wordstars current margin */
while ( !found_index && (fill_line(linebuf) != CONTROLZ)) {
if (scan_line(linebuf) == TRUE) /* look for dot cmds */
continue; /* get next line */
if (this_line >= (page_size - bot_margin)) {
this_line= top_margin;
++this_page;
}
if ((pl + 1) >= MAXENTRIES) {
printf("(Too many entries, ");
break;
}
if (pile < topofpile > (memsize - 200L)) {
printf("Memory buffer full, ");
break;
}
j= 0;
++line_count; /* count total lines,*/
while ((c= linebuf[j++]) != '\0') { /* while not end/line*/
++char_count;
if (c == ' ') {
inword= FALSE; /* end of a word */
if (gotword) { /* if we were looking*/
++pile; /* leave null to mark*/
if (line_num)
sprintf(pile,"%d-%d",this_page,this_line);
else sprintf(pile,"%d",this_page);
while (*pile++);/* point to next */
++entries; /* count another */
gotword= FALSE; /* done with word */
}
} else if (inword == FALSE) { /* non-white char */
inword= TRUE;
++word_count;
}
if (c == WORD_MARK) { /* new word to save */
gotword= TRUE; /* start saving next char */
entry_len= 0;
pointers[pl++]= pile;/* set the pointer */
} else if (c == PHRASE_MARK) {
if (gotphrase) { /* if we had one before, */
++pile; /* let the last null mark it */
if (line_num)
sprintf(pile,"%d-%d",this_page,this_line);
else sprintf(pile,"%d",this_page);
while (*pile++);
++entries; /* counter another */
gotphrase= FALSE;
} else { /* new phrase */
gotphrase= TRUE; /* else start now. */
entry_len= 0; /* just starting */
pointers[pl++]= pile;/* point to it */
}
}
/* see if we should store a character */
else if ( (gotphrase || gotword) &&
(entry_len++ < WORD_LEN) &&
((c != '.') || gotphrase) &&
!(last_char_blank && c == ' ') &&
isprint(c) ) {
*pile++= c; /* were saving now */
*pile= '\0'; /* null terminate it always, */
last_char_blank= (c ==' ' ? TRUE : FALSE);
}
}
++this_line;
}
*pile= CONTROLZ; /* mark the top of the pile, */
pointers[pl]= pile; /* set its pointer */
}
/* Sort routine, straight out of K + R, page 108. */
sort() {
int i,j,gap;
char *temp;
for (gap= pl / 2; gap > 0; gap /= 2) {
for (i= gap; i < pl; i++) {
for (j= i - gap; j >= 0; j-= gap) {
if (comp(pointers[j],pointers[j + gap],4) <= 0)
break;
temp= pointers[j];
pointers[j]= pointers[j + gap];
pointers[j + gap]= temp;
}
}
}
}
/* Quick compare. Does masking instead of tolower(), etc. Only compares
the first 4 characters. */
comp(s1,s2,len)
char *s1,*s2;
int len;
{
int i,v;
for (;i--;) {
if (!*s1) return(0); /* return if end of string */
if (!*s2) return(0);
v= (*s1++ & 0x5f) - (*s2++ & 0x5f);
if (v) break;
}
return(v);
}
/* Dump the pile to the disk. Convert each entry to a single line, making each
an even number of columns wide. Put the page number at the end. Look for
duplicate entries, and remove them. */
dump() {
int column;
int i,x;
char *current_entry;
char *j;
char c;
char last_alpha;
int local_i;
column= 0;
i= 0;
if (found_index == FALSE) { /* dont duplicate this */
sendstr("\r\n..index\r\n");
}
last_alpha= '\0'; /* section header flag */
while (*pointers[i] != CONTROLZ) {
j =pointers[i++]; /* send chars until null */
if (*j !=0xff) { /* if its a deleted dup copy, */
/* skip this */
if (last_alpha != *j) { /* if different alpha */
last_alpha= *j; /* group, do a CR LF */
sendstr("\r\n");
}
current_entry =j; /* else save a copy, */
for (x= lmargin; x > 0; x--) /* tab to left margin */
wrtc(' ',inbuf);
while (c =*j++) {
wrtc (c,inbuf);
++column;
}
while (column++ < LAST_COL) /* make an entry */
wrtc ('.',inbuf); /* tab over, */
wrtc (' ',inbuf); /* type the page # */
while (c =*j++)
wrtc (c,inbuf);
/* Look for duplicate entries; if we find one, remove it (mark 0xff) and
output it's page # on the current one. If more than 5, stop, and start the
rest on the next line. */
local_i =0;
if (*pointers[i] !=CONTROLZ) {
while ((comp(current_entry,pointers[i+local_i]) ==0) && (local_i < 6) ) {
j =pointers[i+local_i++];
*j =0xff; /* mark it removed */
while (*j++); /* skip the entry, */
wrtc (',',inbuf);/* type a comma, */
while (c =*j++) /* send the page # */
wrtc (c,inbuf);
}
}
wrtc (CR,inbuf); /* new line, */
wrtc (LF,inbuf);
column =0; /* next line */
}
}
wrtc (CONTROLZ,inbuf);
}
/* Fill a line buffer with characters. Convert all white_space characters
to a single blank, put a null at the end. */
fill_line(buffer)
char buffer[];
{
int i,n;
char c;
int inblank; /* true to suppress blanks */
long pos;
inblank= FALSE;
i= 0;
while (i < 132) {
n= _xread(inbuf,&c,1);
if (n == 0) return(CONTROLZ); /* check physical end */
c&= 0x7f; /* strip it */
if (c == LF) return(c); /* stop if LF */
if (c == CONTROLZ) { /* dont send the control-z */
_xseek(inbuf,-1L,1); /* back up over it, */
return (c);
}
if (incset(c," ,\t\r;!")) c= ' '; /* white space is space only */
if ((c != ' ') || (inblank == FALSE)) {
buffer[i++]= c;
buffer[i]= '\0';
}
inblank= (c == ' ' ? TRUE : FALSE);
}
return(c);
}
/* Return true if the character is in the string. */
incset(c,s)
char c,*s;
{
while (*s) {
if (c == *s++) return(1);
}
return(0);
}
/* Scan for dot commands. When and if we find a dot command, fool the
caller into thinking that the line we were passed is now empty. (Return TRUE)
If we find the index mark, "..index", set the flag so we dont duplicate
it at dump time. If we find a formfeed, treat it the same as a .pa. */
scan_line(buffer)
char *buffer;
{
int i;
char c,*p,tc[80];
if (*buffer =='.') { /* as per WS specs, must be first atom */
if (comp (buffer,".pa",3) ==0) {
++this_page;
this_line= top_margin;
} else if (comp(buffer,".sh",3) == 0) {
p= next_arg(buffer);
strcpy(tc,p);
printf("%s\r\n",tc);
} else if (comp (buffer,"..index",7) ==0) {
found_index =TRUE;
} else if (comp (buffer,".pn",3) == 0) {
i= get_next_num(buffer); /* get new page num, */
if (i) /* if not zero, */
this_page= i; /* set it, */
} else if (comp (buffer,".pl",3) == 0) {
i= get_next_num(buffer); /* get new paper */
if (i > 5) /* length, */
page_size= i;
} else if (comp (buffer,".mt",3) == 0) {
i= get_next_num(buffer);
if (i == 0)
i= 1;
if (i < 40) /* get top margin, */
top_margin= i; /* set if reasonable */
} else if (comp (buffer,".mb",3) == 0) {
i= get_next_num(buffer);
if (i < page_size)
bot_margin= i;
}
return (TRUE);
}
if (*buffer == FF) {
++this_page;
this_line= top_margin;
return(TRUE);
}
return (FALSE);
}
/* Clean up the pile by converting each first character to upper
case. Deletes leading blanks by adjusting the pointer, but only if
the string is more than 1 char long. */
cleanup()
{
int i;
char c;
i =0;
while ((c= toupper(*pointers[i])) !=CONTROLZ)
{ if ((*pointers[i] ==' ') && (*pointers[i+1] !='\0'))
{ ++(pointers[i]);
c =toupper(*pointers[i]);
}
*pointers[i++] =c;
}
}
/* Send an ascii string to the output file */
sendstr(string)
char *string;
{
_xwrite(inbuf,string,strlen(string));
}
/* Write a character to the file. */
wrtc(c,file)
char c;
int file;
{
_xwrite(file,&c,1);
}
/* Find the next number in the string, return it's value, or 0
if none found. */
get_next_num(string)
char *string;
{
int i;
while (! isdigit(*string))
++string; /* skip leading text, */
return(atoi(string));
}